####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A21, ... A28
###
####################################################################################


library(dplyr)
library(ggplot2)
library(miceadds)
library(AER)
library(rlist)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/05_data_load_clean_absun.R'))


################################################################################
# Plot functions
################################################################################


makeplot = function(model_output){
  temp = model_output
  container = cbind(temp[2:8,], period = c(-4, -3, -2, 0, 1, 2, 3))
  container = rbind(container, c(0, 0, NA, NA, -1)) %>% as.data.frame()
  container = container[order(container$period),]
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period')
  ggplot(data=container, aes(y=beta, x=factor(period)))+
    geom_point() + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), colour="black", width=.1) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application")+
    ylab("") +
    theme_light() +
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}



makeplot_split = function(modelli){
  container = lapply(modelli, function(x) rbind(cbind(x[2:8,], period = c(-4, -3, -2, 0, 1, 2, 3)), c(0,0,NA,NA,-1)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(c('Low Immigration', 'Medium Immigration', 'High Immigration'), each=8))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = c('Low Immigration', 'Medium Immigration', 'High Immigration'))
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f))+
    geom_point(position = position_dodge(width = 0.5)) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light() +
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}


estimation = function(outvar, outname1, outname2){
  
  formula = paste0(outvar, ' ~  m4 + m3 + m2 + p0 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period)')
  
  # Total effect
  df1 = df_did
  temp = lm(data=df1, as.formula(formula))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  makeplot(temp)
  ggsave(paste0(wd_res, '/figures/', outname1, '.pdf'),  height=7, width=10)
  
  # By immigration levels
  models = list()
  for (n in c(1,2,3)){
    print(paste0('Immigation tercile ', n, ' -------------- '))
    df1 = df_did[which(!is.na(df_did$hlm_share) & df_did$imm_quant_99==n),]
    temp = lm(data=df1, as.formula(formula))
    temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
    models[[n]] = temp
  }
  
  makeplot_split(models)
  ggsave(paste0(wd_res, '/figures/', outname2, '.pdf'),  height=7, width=10)
}



################################################################################
# Effect of the policy on home ownership (Figure A21, 22)
################################################################################

estimation('num_res_own/num_log', 'figA21', 'figA22')


################################################################################
# Effects on desirability of housing (Figure A23, 24)
################################################################################

estimation('num_empty_log/num_log', 'figA23', 'figA24')


################################################################################
# effect of the policy on immigration  (Figure A27, 28)
################################################################################

estimation('imm', 'figA27', 'figA28')


################################################################################
# effect of the policy on median income  (Figure A25, 26)
################################################################################

makeplot = function(model_output){
  temp = model_output
  container = cbind(temp[2:7,], period = c(-3, -2, 0, 1, 2, 3))
  container = rbind(container, c(0, 0, NA, NA, -1)) %>% as.data.frame()
  container = container[order(container$period),]
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period')
  ggplot(data=container, aes(y=beta, x=factor(period)))+
    geom_point() + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), colour="black", width=.1) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light() +
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}


makeplot_split = function(modelli){
  container = lapply(modelli, function(x) rbind(cbind(x[2:7,], period = c(-3, -2, 0, 1, 2, 3)), c(0,0,NA,NA,-1)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(c('Low Immigration', 'Medium Immigration', 'High Immigration'), each=7))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = c('Low Immigration', 'Medium Immigration', 'High Immigration'))
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f))+
    geom_point(position = position_dodge(width = 0.5)) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), width=.1, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Periods to Policy Application") + 
    ylab("") +
    theme_light() +
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}


estimation = function(outvar, outname1, outname2){
  
  formula = paste0(outvar, ' ~  m4 + m3 + m2 + p0 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period)')
  
  # Total effect
  df1 = df_did
  temp = lm(data=df1, as.formula(formula))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  makeplot(temp)
  ggsave(paste0(wd_res, '/figures/', outname1, '.pdf'),  height=7, width=10)
  
  # By immigration levels
  models = list()
  for (n in c(1,2,3)){
    print(paste0('Immigation tercile ', n, ' -------------- '))
    df1 = df_did[which(!is.na(df_did$hlm_share) & df_did$imm_quant_99==n),]
    temp = lm(data=df1, as.formula(formula))
    temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
    models[[n]] = temp
  }
  
  makeplot_split(models)
  ggsave(paste0(wd_res, '/figures/', outname2, '.pdf'),  height=7, width=10)
}

estimation('log(median_inc)', 'figA25', 'figA26')


